<template>
  <div class="api-test-container">
    <h1>配送管理API测试页面</h1>
    
    <el-card class="test-card">
      <template #header>
        <div class="card-header">
          <h2>配送管理API测试</h2>
        </div>
      </template>
      
      <!-- 获取配送任务列表 -->
      <div class="api-section">
        <h3>6.1 获取配送任务列表</h3>
        <el-form :model="getTasksForm" label-width="120px">
          <el-form-item label="订单编号">
            <el-input v-model="getTasksForm.order_number" placeholder="请输入订单编号"></el-input>
          </el-form-item>
          <el-form-item label="客户名称">
            <el-input v-model="getTasksForm.customer_name" placeholder="请输入客户名称"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="getTasksForm.status" placeholder="请选择状态" clearable>
              <el-option label="待处理" value="pending"></el-option>
              <el-option label="已分配" value="assigned"></el-option>
              <el-option label="配送中" value="delivering"></el-option>
              <el-option label="已完成" value="completed"></el-option>
              <el-option label="已取消" value="cancelled"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="配送员ID">
            <el-input v-model="getTasksForm.deliveryman_id" placeholder="请输入配送员ID"></el-input>
          </el-form-item>
          <el-form-item label="页码">
            <el-input-number v-model="getTasksForm.page" :min="1"></el-input-number>
          </el-form-item>
          <el-form-item label="每页数量">
            <el-input-number v-model="getTasksForm.limit" :min="1" :max="50"></el-input-number>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleGetTasks">获取</el-button>
          </el-form-item>
        </el-form>
        <div class="api-result">
          <h4>结果：</h4>
          <pre>{{ getTasksResult }}</pre>
        </div>
      </div>
      
      <!-- 获取配送任务详情 -->
      <div class="api-section">
        <h3>6.2 获取配送任务详情</h3>
        <el-form :model="getTaskDetailForm" label-width="120px">
          <el-form-item label="配送任务ID">
            <el-input v-model="getTaskDetailForm.id" placeholder="请输入配送任务ID"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleGetTaskDetail">获取</el-button>
          </el-form-item>
        </el-form>
        <div class="api-result">
          <h4>结果：</h4>
          <pre>{{ getTaskDetailResult }}</pre>
        </div>
      </div>
      
      <!-- 分配配送任务 -->
      <div class="api-section">
        <h3>6.3 分配配送任务</h3>
        <el-form :model="assignTaskForm" label-width="120px">
          <el-form-item label="配送任务ID">
            <el-input v-model="assignTaskForm.id" placeholder="请输入配送任务ID"></el-input>
          </el-form-item>
          <el-form-item label="配送员ID">
            <el-input v-model="assignTaskForm.deliveryman_id" placeholder="请输入配送员ID"></el-input>
          </el-form-item>
          <el-form-item label="优先级">
            <el-select v-model="assignTaskForm.priority" placeholder="请选择优先级">
              <el-option label="高" value="high"></el-option>
              <el-option label="中" value="medium"></el-option>
              <el-option label="低" value="low"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="预计配送时间">
            <el-date-picker v-model="assignTaskForm.estimated_delivery_time" type="datetime" placeholder="请选择预计配送时间"></el-date-picker>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="assignTaskForm.note" type="textarea" placeholder="请输入备注"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleAssignTask">分配</el-button>
          </el-form-item>
        </el-form>
        <div class="api-result">
          <h4>结果：</h4>
          <pre>{{ assignTaskResult }}</pre>
        </div>
      </div>
      
      <!-- 更新配送任务状态 -->
      <div class="api-section">
        <h3>6.4 更新配送任务状态</h3>
        <el-form :model="updateTaskStatusForm" label-width="120px">
          <el-form-item label="配送任务ID">
            <el-input v-model="updateTaskStatusForm.id" placeholder="请输入配送任务ID"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="updateTaskStatusForm.status" placeholder="请选择状态">
              <el-option label="配送中" value="delivering"></el-option>
              <el-option label="已完成" value="completed"></el-option>
              <el-option label="已取消" value="cancelled"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="位置">
            <el-input v-model="updateTaskStatusForm.location" placeholder="请输入位置"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="updateTaskStatusForm.note" type="textarea" placeholder="请输入备注"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleUpdateTaskStatus">更新</el-button>
          </el-form-item>
        </el-form>
        <div class="api-result">
          <h4>结果：</h4>
          <pre>{{ updateTaskStatusResult }}</pre>
        </div>
      </div>
      
      <!-- 获取配送员列表 -->
      <div class="api-section">
        <h3>6.5 获取配送员列表</h3>
        <el-button type="primary" @click="handleGetDeliveryStaff">获取配送员列表</el-button>
        <div class="api-result">
          <h4>结果：</h4>
          <pre>{{ getDeliveryStaffResult }}</pre>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { getToken } from '@/utils/auth'

// 获取配送任务列表
const getTasksForm = reactive({
  order_number: '',
  customer_name: '',
  status: '',
  deliveryman_id: '',
  page: 1,
  limit: 10
})
const getTasksResult = ref('')

const handleGetTasks = async () => {
  try {
    const token = getToken()
    if (!token) {
      ElMessage.warning('请先登录')
      return
    }
    
    const params = {}
    if (getTasksForm.order_number) params.order_number = getTasksForm.order_number
    if (getTasksForm.customer_name) params.customer_name = getTasksForm.customer_name
    if (getTasksForm.status) params.status = getTasksForm.status
    if (getTasksForm.deliveryman_id) params.deliveryman_id = getTasksForm.deliveryman_id
    if (getTasksForm.page) params.page = getTasksForm.page
    if (getTasksForm.limit) params.limit = getTasksForm.limit
    
    const response = await axios.get('/api/delivery-tasks', {
      params,
      headers: {
        'Authorization': `Bearer ${token}`
      }
    })
    getTasksResult.value = JSON.stringify(response.data, null, 2)
    ElMessage.success('获取成功')
  } catch (error) {
    getTasksResult.value = JSON.stringify(error.response?.data || error.message, null, 2)
    ElMessage.error('获取失败')
  }
}

// 获取配送任务详情
const getTaskDetailForm = reactive({
  id: ''
})
const getTaskDetailResult = ref('')

const handleGetTaskDetail = async () => {
  try {
    const token = getToken()
    if (!token) {
      ElMessage.warning('请先登录')
      return
    }
    
    if (!getTaskDetailForm.id) {
      ElMessage.warning('请输入配送任务ID')
      return
    }
    
    const response = await axios.get(`/api/delivery-tasks/${getTaskDetailForm.id}`, {
      headers: {
        'Authorization': `Bearer ${token}`
      }
    })
    getTaskDetailResult.value = JSON.stringify(response.data, null, 2)
    ElMessage.success('获取成功')
  } catch (error) {
    getTaskDetailResult.value = JSON.stringify(error.response?.data || error.message, null, 2)
    ElMessage.error('获取失败')
  }
}

// 分配配送任务
const assignTaskForm = reactive({
  id: '',
  deliveryman_id: '',
  priority: 'medium',
  estimated_delivery_time: '',
  note: ''
})
const assignTaskResult = ref('')

const handleAssignTask = async () => {
  try {
    const token = getToken()
    if (!token) {
      ElMessage.warning('请先登录')
      return
    }
    
    if (!assignTaskForm.id) {
      ElMessage.warning('请输入配送任务ID')
      return
    }
    
    if (!assignTaskForm.deliveryman_id) {
      ElMessage.warning('请输入配送员ID')
      return
    }
    
    const data = {
      deliveryman_id: assignTaskForm.deliveryman_id,
      priority: assignTaskForm.priority,
      note: assignTaskForm.note
    }
    
    if (assignTaskForm.estimated_delivery_time) {
      data.estimated_delivery_time = assignTaskForm.estimated_delivery_time
    }
    
    const response = await axios.post(`/api/delivery-tasks/${assignTaskForm.id}/assign`, data, {
      headers: {
        'Authorization': `Bearer ${token}`
      }
    })
    assignTaskResult.value = JSON.stringify(response.data, null, 2)
    ElMessage.success('分配成功')
  } catch (error) {
    assignTaskResult.value = JSON.stringify(error.response?.data || error.message, null, 2)
    ElMessage.error('分配失败')
  }
}

// 更新配送任务状态
const updateTaskStatusForm = reactive({
  id: '',
  status: '',
  location: '',
  note: ''
})
const updateTaskStatusResult = ref('')

const handleUpdateTaskStatus = async () => {
  try {
    const token = getToken()
    if (!token) {
      ElMessage.warning('请先登录')
      return
    }
    
    if (!updateTaskStatusForm.id) {
      ElMessage.warning('请输入配送任务ID')
      return
    }
    
    if (!updateTaskStatusForm.status) {
      ElMessage.warning('请选择状态')
      return
    }
    
    const data = {
      status: updateTaskStatusForm.status,
      location: updateTaskStatusForm.location,
      note: updateTaskStatusForm.note
    }
    
    const response = await axios.post(`/api/delivery-tasks/${updateTaskStatusForm.id}/status`, data, {
      headers: {
        'Authorization': `Bearer ${token}`
      }
    })
    updateTaskStatusResult.value = JSON.stringify(response.data, null, 2)
    ElMessage.success('更新成功')
  } catch (error) {
    updateTaskStatusResult.value = JSON.stringify(error.response?.data || error.message, null, 2)
    ElMessage.error('更新失败')
  }
}

// 获取配送员列表
const getDeliveryStaffResult = ref('')

const handleGetDeliveryStaff = async () => {
  try {
    const token = getToken()
    if (!token) {
      ElMessage.warning('请先登录')
      return
    }
    
    const response = await axios.get('/api/delivery-staff', {
      headers: {
        'Authorization': `Bearer ${token}`
      }
    })
    getDeliveryStaffResult.value = JSON.stringify(response.data, null, 2)
    ElMessage.success('获取成功')
  } catch (error) {
    getDeliveryStaffResult.value = JSON.stringify(error.response?.data || error.message, null, 2)
    ElMessage.error('获取失败')
  }
}
</script>

<style scoped>
.api-test-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.test-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.api-section {
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #ebeef5;
}

.api-section:last-child {
  border-bottom: none;
}

.api-result {
  margin-top: 15px;
  background-color: #f5f7fa;
  padding: 10px;
  border-radius: 4px;
}

pre {
  white-space: pre-wrap;
  word-wrap: break-word;
  margin: 0;
}
</style>